% clear;clc
% t=pi/10;
% t2=-pi/10;
% x=0:pi/20:pi;
% x2=3*pi:pi/20:4*pi;
% m=moviein(31);
% for i=1:31
%     y=sin(x-(i-1)*t);
%     y2=sin(x2-(i-1)*t2+pi);
%     plot(x2,y2,'k');
%     hold on
%     plot(x,y,'k');
%     hold off
%     x=x+t;
%     x2=x2+t2;
%     axis([0,4*pi,0,2]);
%     m(:,i)=getframe;
% end
% movie(m,1,10)


% x=-10:.025:10;
% for t=-7:.05:7
% plot(x,(x+1+t).*(-1<x+t&x+t<=0)+(-x+1-t).*(0<x+t&x+t<1)+(x+1-t).*(-1<x-t&x-t<=0)+(-x+1+t).*(0<x-t&x-t<1),[-7 7],[0 0],'r.');
% text([-7 7],[0 0],{'A','B'},'HorizontalAlignment','center','VerticalAlignment','top','fontunits','normalized','fontsize',.1);
% axis([-10,10,-1,3]);
% drawnow
% end

clear
x=-10:0.2:10;
t=0;
y1=10*exp(-(x-t+7).^2);     %产生正向波
y2=5*exp(-(x+t-7).^2);       %产生负向波
y=y1+y2;
figure
subplot(3,1,1)
h1=plot(x,y1);%画正向波并取句柄
title('波的叠加原理','Fontsize',14)
text(-0.8,13,'正向波','Fontsize',14)%标题及说明
axis([-10,10,-0.5,15])      %设定坐标范围
grid on 
hold on
subplot(3,1,2)
h2=plot(x,y2,'g');
text(-0.8,13,'负向波','FontSize',14)
axis([-10,10,-0.5,15])
grid on
hold on
subplot(3,1,3)
h3=plot(x,y,'r');
axis([-10,10,-0.5,15])
grid on
hold on
pause                          %暂停，按任意健开始演示动画
t=0:0.15:15;
for i=1:length(t)              %以下为动画设置
    y1=10*exp(-(x-t(i)+7).^2);
    set(h1,'YData',y1);
    drawnow;
    y2=5*exp(-(x+t(i)-7).^2);
    set(h2,'YData',y2);
    drawnow;
    y=10*exp(-(x-t(i)+7).^2)+5*exp(-(x+t(i)-7).^2);
    set(h3,'YData',y);
    pause(0.01)
    drawnow
end